package com.kangyi.admin.service.Impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kangyi.admin.mapper.AdminUserMapper;
import com.kangyi.admin.mapper.NursingHomeBookingMapper;
import com.kangyi.admin.service.NursingHomeBookingService;
import com.kangyi.constant.MessageConstant;
import com.kangyi.context.BaseContext;
import com.kangyi.dto.NursingHomeBookingPageQueryDTO;
import com.kangyi.dto.RejectBookingDTO;
import com.kangyi.entity.NursingHomeBooking;
import com.kangyi.exception.DataNotExistException;
import com.kangyi.exception.PermissionsException;
import com.kangyi.result.BookingPageResult;
import com.kangyi.vo.AdminUserVO;
import com.kangyi.vo.NursingHomeBookingDetailVO;
import com.kangyi.vo.NursingHomeBookingPageQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Blank
 * @version 1.0
 */
@Service
public class NursingHomeBookingServiceImpl implements NursingHomeBookingService {

    @Autowired
    private NursingHomeBookingMapper nursingHomeBookingMapper;

    @Autowired
    private AdminUserMapper adminUserMapper;


    // 调用后检查当前线程id是否有足够的权限进行后续操作
    public void validateUserPermissions() {
        Integer currentId = BaseContext.getCurrentId();
        AdminUserVO adminUserVO = adminUserMapper.selectById(currentId);
        // 判断权限是否既不是1也不是2，如果是则抛出异常
        if (!adminUserVO.getPermissions().equals(1) && !adminUserVO.getPermissions().equals(2)) {
            throw new PermissionsException(MessageConstant.INSUFFICIENT_PRIVILEGE);
        }
    }

    @Override
    public BookingPageResult pageQuery(NursingHomeBookingPageQueryDTO nursingHomeBookingPageQueryDTO) {
        validateUserPermissions();
        PageHelper.startPage(nursingHomeBookingPageQueryDTO.getPage(), nursingHomeBookingPageQueryDTO.getPageSize());
        Page<NursingHomeBookingPageQueryVO> page = nursingHomeBookingMapper.pageQuery(nursingHomeBookingPageQueryDTO);
        long total = page.getTotal();
        List<NursingHomeBookingPageQueryVO> result = page.getResult();

        // 初始化不同状态的数量
        int pendingReview = 0;
        int agree = 0;
        int reject = 0;

        // 遍历结果列表，统计不同状态的数量
        for (NursingHomeBookingPageQueryVO vo : result) {
            Integer status = vo.getStatus();
            if (status != null) {
                if (status == 0) {
                    pendingReview++;
                } else if (status == 1) {
                    agree++;
                } else if (status == 2) {
                    reject++;
                }
            }
        }

        return new BookingPageResult(total, result, pendingReview, agree, reject);
    }

    @Override
    public void deleteBooking(List<Integer> ids) {
        validateUserPermissions();
        nursingHomeBookingMapper.deleteBooking(ids);
    }

    @Override
    public void agreeBooking(List<Integer> ids) {
        validateUserPermissions();
        for (Integer id : ids) {
            NursingHomeBooking nursingHomeBooking = nursingHomeBookingMapper.getById(id);
            if (nursingHomeBooking.getStatus().equals(1)) {
                throw new DataNotExistException(id+MessageConstant.DO_NOT_REPEAT_THE_OPERATION);
            }
        }
        List<NursingHomeBooking> nursingHomeBookingList = nursingHomeBookingMapper.getByIds(ids);
        if (!CollUtil.isEmpty(nursingHomeBookingList)) {
            List<NursingHomeBooking> collect = nursingHomeBookingList.stream().map(nursingHomeBooking ->
                    NursingHomeBooking
                            .builder()
                            .id(nursingHomeBooking.getId())
                            .status(NursingHomeBooking.AGREE)
                            .reason(null)
                            .build()
            ).collect(Collectors.toList());
            nursingHomeBookingMapper.updateStatus(collect);
        }
    }

    @Override
    public void rejectBooking(RejectBookingDTO rejectBookingDTO) {
        validateUserPermissions();
        NursingHomeBooking nursingHomeBooking = NursingHomeBooking.builder()
                .id(rejectBookingDTO.getId())
                .status(NursingHomeBooking.REFUSE)
                .reason(rejectBookingDTO.getReason())
                .build();
        nursingHomeBookingMapper.rejectBooking(nursingHomeBooking);
    }

    @Override
    public NursingHomeBookingDetailVO BookingDetail(Integer id) {
        validateUserPermissions();
        return nursingHomeBookingMapper.getBookingDetail(id);
    }

}
